package com.rmfyzxfw.caseprocess.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rmfyzxfw.caseprocess.enums.DynamicDatabaseTypeEnum;
import com.rmfyzxfw.caseprocess.mapper.DynamicDataSourceMapper;
import com.rmfyzxfw.caseprocess.mapper.EngineDataDynamicTableMySqlMapper;
import com.rmfyzxfw.caseprocess.mapper.EngineDataDynamicTableOracleMapper;
import com.rmfyzxfw.caseprocess.model.po.DynamicDataSource;
import com.rmfyzxfw.caseprocess.model.vo.ColumnInfoVO;
import com.rmfyzxfw.caseprocess.model.vo.TableInfoVO;
import com.rmfyzxfw.caseprocess.service.DynamicDataSourceService;
import com.rmfyzxfw.caseprocess.util.DynamicDataSourceUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author GYW
* @description 针对表【t_dynamic_data_source(动态数据源)】的数据库操作Service实现
* @createDate 2024-08-14 16:11:02
*/
@Service
@RequiredArgsConstructor
public class DynamicDataSourceServiceImpl extends ServiceImpl<DynamicDataSourceMapper, DynamicDataSource>
    implements DynamicDataSourceService{
    private final EngineDataDynamicTableMySqlMapper dataDynamicTableMySqlMapper;
    private final EngineDataDynamicTableOracleMapper dataDynamicTableOracleMapper;
    @Override
    public List<TableInfoVO> getTables(String dynamicDataSourceId) {
        DynamicDataSource dynamicDataSource = this.getOne(Wrappers.<DynamicDataSource>lambdaQuery().eq(DynamicDataSource::getDynamicDataSourceId, dynamicDataSourceId));
        List<TableInfoVO> tableInfoVOS;
        if (DynamicDatabaseTypeEnum.MYSQL.equals(DynamicDatabaseTypeEnum.ofCode(dynamicDataSource.getDatabaseType()))) {
            tableInfoVOS = DynamicDataSourceUtil.route(dynamicDataSourceId, () -> dataDynamicTableMySqlMapper.getTableList(dynamicDataSource.getDbName()));
        }else { // Oracle
            tableInfoVOS = DynamicDataSourceUtil.route(dynamicDataSourceId, () -> dataDynamicTableOracleMapper.getTableList(dynamicDataSource.getDbName()));
        }
        return tableInfoVOS;
    }

    @Override
    public List<ColumnInfoVO> getColumns(String dynamicDataSourceId, String tableName) {
        DynamicDataSource dynamicDataSource = this.getOne(Wrappers.<DynamicDataSource>lambdaQuery().eq(DynamicDataSource::getDynamicDataSourceId, dynamicDataSourceId));
        List<ColumnInfoVO> columnInfoVOS;
        if (DynamicDatabaseTypeEnum.MYSQL.equals(DynamicDatabaseTypeEnum.ofCode(dynamicDataSource.getDatabaseType()))) {
            columnInfoVOS = DynamicDataSourceUtil.route(dynamicDataSourceId, () -> dataDynamicTableMySqlMapper.getColumnList(tableName));
        }else { // Oracle
            columnInfoVOS = DynamicDataSourceUtil.route(dynamicDataSourceId, () -> dataDynamicTableOracleMapper.getColumnList(tableName));
        }
        return columnInfoVOS;
    }
}




